package com.joy.web.admin.controller;


import com.joy.common.exception.JsonException;
import com.joy.common.exception.PageException;
import com.joy.common.model.ApiResponse;
import com.joy.common.model.vo.ResourceVO;
import com.joy.web.admin.entity.Resource;
import com.joy.web.admin.service.IResourceService;
import com.joy.web.admin.service.IUserService;
import com.joy.common.enums.Status;
import com.joy.web.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源管理
 * @author yjzhao
 * @since 2019-09-16
 */
@Slf4j
@Controller
@RequestMapping("/admin/resource")
public class ResourceController extends BaseController {

    @javax.annotation.Resource
    IResourceService resourceService;
    @javax.annotation.Resource
    IUserService userService;

    /**
     * 用户管理初始化页面
     * @return
     */
    @GetMapping(value = {"", "/", "/index" })
    public String index(ModelMap map) {
        try {
            List<ResourceVO> resourceTree = resourceService.getResourceTree();
            map.addAttribute("tree", resourceTree);
            return "pages/admin/resource/index";
        } catch (Exception e) {
            log.info("/admin/resource/index MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "获取资源树报错");
        }

    }


    /**
     * 编辑
     * @return
     */
    @GetMapping(value = { "/add" })
    public String add(ModelMap map) {
        return "pages/admin/resource/add";
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/addSave" })
    @ResponseBody
    public ApiResponse addSave(@ModelAttribute("resource") Resource resource) {

        try {
            //更新用户详情
            boolean upSucc = resourceService.save(resource);
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/resource/addSave MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "新增资源报错");
        }

    }

    /**
     * 编辑
     * @return
     */
    @GetMapping(value = { "/edit/{resourceId}" })
    public String edit(@PathVariable(value="resourceId") String resourceId, ModelMap map) {

        try {
            // 根据 userId 获取 用户详情
            Resource resource = resourceService.getById(resourceId);
            //传至前端
            map.put("resource", resource);
            //用户编辑页
            return "pages/admin/resource/edit";
        } catch (Exception e) {
            log.info("/admin/resource/edit MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "根据id获取资源报错");
        }

    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/update" })
    @ResponseBody
    public ApiResponse update(@ModelAttribute("resource") Resource resource) {

        try {
            //更新用户详情
            boolean upSucc = resourceService.updateById(resource);
            userService.removeUserAuthorization();
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/resource/edit MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "更新资源信息出错了");
        }
    }

}
